package nl.hanze.monoalphabetic.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import nl.hanze.monoalphabetic.model.TextStatistics;

/**
 *
 * @author alex
 */
public class MainController {

    private Random rand;
    private HashMap<Character, Character> conversion;
    private TextStatistics referenceStatistics;
    private TextStatistics compareStatistics;

    public MainController() {
        //Maak random generator aan met seed
        rand = new Random(1000);
        conversion = new HashMap<>(26);
        referenceStatistics = new TextStatistics();
        compareStatistics = new TextStatistics();

    }

    /**
     * Lees bestand in
     *
     * @param file
     */
    public void analyzeFile(File file) {
        FileInputStream fis = null;

        try {
            fis = new FileInputStream(file);

        } catch (FileNotFoundException ex) {
            Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
        }


        StringBuilder b = new StringBuilder();
        Scanner scanner = new Scanner(fis);
        while (scanner.hasNextLine()) {
            b.append(scanner.nextLine());
        }

        try {
            fis.close();
        } catch (IOException ex) {
            Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
        }

        referenceStatistics.generateStatistics(b.toString());
    }


    /**
     * Functie om automatisch een conversietabel te maken voor een
     * monoalfabetische conversie. Alle letters worden random gekoppeld aan een
     * andere letter.
     */
    public void generateConversionTable() {
        List<Character> alphabet = new LinkedList<>();
        //Zet alphabet in lijst
        for (int i = 0; i < 26; i++) {
            alphabet.add((char) ('a' + i));
        }

        for (int i = 0; i < 26; i++) {
            //Genereer random nummer in bereik van overgebleven letters
            int random = (int) (rand.nextFloat() * (alphabet.size()));
            //Zet gekozen letter in conversie tabel
            conversion.put((char) ('a' + i), alphabet.remove(random));
        }
    }

    /**
     * Method om text met behulp van conversietabel om te zetten.
     *
     * @param text
     * @return
     */
    public String scrambleText(String text) {
        StringBuilder output = new StringBuilder();

        for (Character c : text.toCharArray()) {
            c = Character.toLowerCase(c);

            if (c >= 'a' && c <= 'z') {
                output.append(conversion.get(c));
            } else {
                output.append(c);
            }
        }
        return output.toString();
    }

    /**
     * Methode om text om te zetten naar leesbare text.
     *
     * @param text
     * @param deConverse
     * @return
     */
    public String deScrambleText(String text, HashMap<Character, Character> deConverse) {

        StringBuilder output = new StringBuilder();
        //zet hoofdletters en letters om, rest niet
        for (Character c : text.toCharArray()) {
            c = Character.toLowerCase(c);

            if (c >= 'a' && c <= 'z') {
                output.append(deConverse.get(c));
            } else {
                output.append(c);
            }
        }
        return output.toString();
    }


    public HashMap<Character, Character> getConversion() {
        return conversion;
    }

    public TextStatistics getCompareStatistics() {
        return compareStatistics;
    }

    public TextStatistics getReferenceStatistics() {
        return referenceStatistics;
    }
}
